package com.um.jdy.app.base.manager;

import cn.hutool.core.bean.BeanUtil;
import com.um.jdy.app.base.entity.vo.AboutVO;
import com.um.jdy.app.base.entity.vo.AreaTreeNode;
import com.um.jdy.common.base.entity.po.Area;
import com.um.jdy.common.base.entity.vo.BaseInfoVO;
import com.um.jdy.common.base.enums.YesOrNo;
import com.um.jdy.common.base.service.AreaService;
import com.um.jdy.common.base.service.SettingService;
import com.um.jdy.common.platform.entity.po.Agreement;
import com.um.jdy.common.platform.entity.po.AppSetting;
import com.um.jdy.common.platform.entity.vo.AgreementVO;
import com.um.jdy.common.platform.service.AgreementService;
import com.um.jdy.common.platform.service.AppSettingService;
import com.um.springboot.starter.orm.entity.Response;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Hashtable;
import java.util.List;
import java.util.Map;

/**
 * @Title
 * @Description
 * @Author renjunwei
 * @Date 2023/5/22 10:37
 */
@Slf4j
@Service
public class AppManager {

    @Autowired
    AppSettingService appSettingService;

    @Autowired
    SettingService settingService;

    @Autowired
    AreaService areaService;

    @Autowired
    AgreementService agreementService;

    /**
     * 查询小程序信息
     * @return
     */
    public AboutVO getAbout() {
        AppSetting appSetting = appSettingService.getEntity("com_id", appSettingService.getComId());
        BaseInfoVO baseInfo = settingService.getBaseInfo();

        AboutVO vo = BeanUtil.toBean(appSetting, AboutVO.class);
        vo.setVersion(baseInfo.getVersion());

        return vo;
    }

    /**
     * 查询平台协议
     * @return
     */
    public AgreementVO getAgreement(){
        Agreement agreement = agreementService.getEntity("com_id", appSettingService.getComId());
        if(null == agreement){
            AgreementVO vo = new AgreementVO();
            vo.setIsEnable(YesOrNo.No.name());
            return vo;
        }

        return BeanUtil.toBean(agreement, AgreementVO.class);
    }

    public List<AreaTreeNode> getAreaTree() {
        List<Area> listProvince = areaService.getList(new String[]{"deep"}, new Object[]{"1"}, new String[]{"sort_no"}, new String[]{"asc"});
        List<Area> listCity = areaService.getList(new String[]{"deep"}, new Object[]{"2"}, new String[]{"sort_no"}, new String[]{"asc"});
        List<Area> listArea = areaService.getList(new String[]{"deep"}, new Object[]{"3"}, new String[]{"sort_no"}, new String[]{"asc"});

        Map<String, List<AreaTreeNode>> mapCityChildren = new Hashtable<>();
        for (Area area : listArea) {

            AreaTreeNode areaNode = BeanUtil.copyProperties(area, AreaTreeNode.class);
            String parentId = areaNode.getBaseAreaParentId();

            List list = null;
            if (mapCityChildren.containsKey(parentId)) {
                list = mapCityChildren.get(parentId);
            } else {
                list = new ArrayList();
                mapCityChildren.put(parentId, list);
            }
            list.add(areaNode);
        }

        Map<String, List<AreaTreeNode>> mapProvinceChildren = new Hashtable<>();

        for (Area city : listCity) {
            AreaTreeNode cityNode = BeanUtil.copyProperties(city, AreaTreeNode.class);
            String id = city.getBaseAreaId();
            if (mapCityChildren.containsKey(id)) {
                cityNode.setChildren(mapCityChildren.get(id));
            }

            String parentId = cityNode.getBaseAreaParentId();
            List list = null;
            if (mapProvinceChildren.containsKey(parentId)) {
                list = mapProvinceChildren.get(parentId);
            } else {
                list = new ArrayList();
            }
            list.add(cityNode);
            mapProvinceChildren.put(parentId, list);
        }

        List<AreaTreeNode> returnList = new ArrayList<>();
        for (Area province : listProvince) {
            AreaTreeNode provinceNode = BeanUtil.copyProperties(province, AreaTreeNode.class);

            String id = provinceNode.getBaseAreaId();
            if (mapProvinceChildren.containsKey(id)) {
                provinceNode.setChildren(mapProvinceChildren.get(id));
            } else {
                provinceNode.setChildren(new ArrayList<>());
            }
            returnList.add(provinceNode);
        }

        return returnList;
    }


}
